import { GetHiddenMenuList } from "@/apis/menu";
import { getExpiresTime } from "@/common/auth/token";
import { Encrypt } from "@/common/encryptHelper";
import { useGlobalContent } from "@/store";
import { Form, Input } from "antd";
import { AntDraggableModal } from "hy_dev_lib";
import Cookies from "js-cookie";

interface ISystemConfigPassword {
  open: 0 | 1;
  setOpen: (open: 0 | 1) => any;
  cb: () => void;
}

const SystemConfigPassword = (props: ISystemConfigPassword) => {
  const { open, setOpen, cb } = props;
  const [form] = Form.useForm();
  const { globalDispatch, global } = useGlobalContent();

  const onSubmit = async () => {
    const formValues = await form.validateFields();
    Cookies.set("showSystemConfigMenu", Encrypt(formValues.code), { expires: getExpiresTime() });
    // Cookies.set("showSystemConfigMenu", formValues.code, { expires: getExpiresTime() });
    try {
      const data = await GetHiddenMenuList();
      globalDispatch({
        type: "MENU_LIST",
        data: [...(global.MENU_LIST || []), ...data],
      });
      // if (data !== true) return openMessage({
      //   type: "error",
      //   content: "口令错误！"
      // })
      onClose();
      // cb && cb();
    } catch (error) {
      Cookies.remove("showSystemConfigMenu");
    }
  };

  const onClose = () => {
    form.resetFields();
    setOpen(0);
  };

  return (
    <AntDraggableModal destroyOnClose open={open > 0} onCancel={onClose} onOk={onSubmit} title={"口令"}>
      <Form form={form} labelCol={{ span: 5 }} wrapperCol={{ span: 19 }}>
        <Form.Item
          style={{ paddingTop: 6 }}
          label="口令"
          name={"code"}
          rules={[{ required: true, message: "请输入口令!" }]}
        >
          <Input.Password autoFocus autoComplete="new-password" placeholder="请输入口令" />
        </Form.Item>
      </Form>
    </AntDraggableModal>
  );
};
export default SystemConfigPassword;
